<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';

//Người tạo : Lại Ngọc Thái Châu - Team 0
//Module 	: config
//Chức năng	: Quản lý config cua default page

class Admin_SettingController extends BaseAdminController {
		
	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$user->check_login();
		$user->permission();
	}
	
	public function indexAction(){
			$dateInt = new Util();
			$tbparameter = new Admin_Model_Parameter();
			
			if(isset($_POST) && $_POST!=null)
			{
				$pattern = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
				$mailStr = $_POST['config_email'];
				if(preg_match($pattern, $mailStr)==0) {
				    $this->view->warning ="Wrong email structure";  
				}
				else 
				{
					if(!is_numeric($_POST['config_telephone']))
					{
						$this->view->warning ="Wrong phone structure";
					}
					else
					{
						if(!is_numeric($_POST['config_fax']))
						{
							$this->view->warning ="Wrong fax structure";
						} 
						else 
						{
							$setting = array(
											"parameter_name" => "config",
											"parameter_value"  => serialize($_POST),
									
							);
							$config = $tbparameter->selectSetting("config");
							if(count($config) > 0)
							{
								$tbparameter->updateSetting($setting);
								$this->view->success ="Updated success";
							}
							else 
							{
								$tbparameter->insertSetting($setting);
								$this->view->success ="Inserted success";
							}
						}	
					}	
				}
			} 

			$currentpage = $tbparameter->selectSetting("config");
			$template = $tbparameter->selectSetting("template");
			$this->view->currentsetting = unserialize($currentpage[0]["parameter_value"]);
			$this->view->template = unserialize($template[0]["parameter_value"]);		
	}
	
	public function templateAction() {
		$template = basename($_GET['template']);
		if (file_exists(DIR_IMAGE . 'templates/' . $template . '.png')) {
			$image = HTTP_IMAGE . 'templates/' . $template . '.png';
		} else {
			$image = HTTP_IMAGE . 'no_image.jpg';
		}
		$this->_helper->layout->disableLayout();
		$this->view->image =$image;
	}	
	public function insertAction()
	{
		$tbparameter = new Admin_Model_Parameter();
		$layour = array("layoutmain","layoutmain_inv","layoutmain_web1","layoutmain_web2"
			);
			$setting = array(
											"parameter_name" => "template",
											"parameter_value"  => serialize($layour),
			);
			
			$tbparameter->updateSetting($setting);
			$this->_helper->layout->disableLayout();
			
	}
}